package edu.ccut.saturn.component.loader;

import java.net.URL;


import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.IWorkFlowInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;
import edu.ccut.saturn.component.impl.SaturnWorkFlowInfo;

/**
 * 工作流配置文件的加载类，加载后生成类型<code>IWorkFlowInfo</code>的实例。
 * 
 * @author 温建
 * 
 * @version 1.0 2008.9.02
 * 
 * @see IWorkFlowInfo
 * 
 */
public class WorkFlowLoader {

	private static final String LOGGER_KEY_LOAD_BUNDLE = SaturnLoggerManager.LOGGER_KEY_LOAD_BUNDLE;

	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_LOAD_BUNDLE);

	/**
	 * 加载工作流配置文件。需要bundle的符号名称、配置文件名称和配置文件的url信息。
	 * @param bundle
	 * @param workFlowFile
	 * @return
	 * @throws SaturnComponentException
	 */
	public static IWorkFlowInfo loadWorkFlowInfo(Bundle bundle,
			String workFlowFile) throws SaturnComponentException {

		try {
			URL url = bundle.getEntry(workFlowFile);
			logger.debug("START LOAD WORKFLOW \"" + url + "\"");
			return new SaturnWorkFlowInfo(bundle.getSymbolicName(),
					workFlowFile, url);
		
		} catch (Exception e) {
			throw new SaturnComponentException("load file failed in bundle[key="
					+ bundle.getSymbolicName() + "] with file name ["
					+ workFlowFile + "]", e);
		
		} finally {
			logger.debug("END LOAD WORKFLOW ");
			logger.debug("");
		}
	}
}
